package fr.playsoft.psas;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.image.SmartImageView;

public class RemoteGallery extends Activity {
	
	private ImageAdapter mAdapter;
	static ImageDownloader sImageDownloader;
	
	private static final String[] URL_DATA = {
		"http://www.apple-touch.com/wp-content/uploads/2007/11/android_ipod_touch_2.jpg",
		"http://img.weather.weatherbug.com/images/tools/mobile/android-application.jpg",
		"http://ostatic.com/files/images/android1.jpg",
		"http://t2.gstatic.com/images?q=tbn:ANd9GcR9M0aFwu5B0M9A-u-FOZ3PfFQXpYmHR5oPiNjB6zkwuEmfSBUao2RaWZ-iKw",
		"http://allandroidblog.com/wp-content/uploads/2011/09/Securing-your-Android.jpg",
		"http://2.bp.blogspot.com/-A84PcnZNjEA/Th8_-qQpTsI/AAAAAAAAAQ0/EOsZ_ontOyk/s1600/htc-magic-the-g2-android-phone-for-vodafone.jpg",
		"http://mobileandroidphone.tk/wp-content/uploads/Android-phone-to-pc-2.jpg",
		"http://android.smartphoneresour.netdna-cdn.com/wp-content/uploads/2011/10/android-chart.png",
		"http://www.gadgetlite.com/wp-content/uploads/2010/04/android.png",
		"http://reviews.in.88db.com/images/icd-ultra-android-tablet_1.jpg",
		"http://wccftech.com/wp-content/uploads/2011/05/NetflixAndroid.jpg",
		"http://www.androidsmartphones.net/wp-content/uploads/2011/10/htc-evo-4g.jpg",
		"http://techreviews.in/wp-content/uploads/2010/12/android-email.jpg",
		"http://3.bp.blogspot.com/-x7_EPrhTzig/Th8__QmxH0I/AAAAAAAAAQ4/GDCEF15NwQY/s1600/htc-unveils-the-first-android-powered-htc-dream-in-australia.png",
		"http://topnews.net.nz/images/Amazon-Android-App-Store.jpg",
		"http://www.videoconverterfactory.com/tips/imgs/why-4.jpg",
		"http://4.bp.blogspot.com/-MUbqGLkQIk4/ToC_nabcd0I/AAAAAAAAAzY/OyuP98OcSxI/s1600/android+andbook.jpg",
		"http://3.bp.blogspot.com/-LqjFsnYIzYI/ToC_bNzyZQI/AAAAAAAAAzU/OM0Ezhbn-jI/s1600/android+cookbook.jpg",
		"http://cdn.androidcommunity.com/wp-content/uploads/2009/01/android_back_to_school.jpg",
		"http://images.maketecheasier.com/2010/01/android-sdk-eclipse5.png"
	};
    
    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        setContentView(R.layout.imagegallery_layout);
        
        sImageDownloader = new ImageDownloader(this);
        
        fillGallery();
        
    }
    
    private void fillGallery() {
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		mAdapter = new ImageAdapter(this, URL_DATA);
		gallery.setAdapter(mAdapter);
		
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Toast.makeText(RemoteGallery.this, "Click on item " + position, Toast.LENGTH_SHORT).show();
				
			}
		});
		
	}

	public class ImageAdapter extends BaseAdapter {
    	private Context mContext;
    	private String[] mImageUrl;
    	public ImageAdapter(Context context, String[] imgUrl) {
    		mContext = context;
    		mImageUrl = imgUrl;
    	}
    	
    	@Override
    	public int getCount() {
    		return mImageUrl.length;
    	}

    	@Override
    	public Object getItem(int arg0) {
    		// TODO Auto-generated method stub
    		return null;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		// TODO Auto-generated method stub
    		return 0;
    	}

    	@Override
    	public View getView(int position, View view, ViewGroup parentView) {
    		Holder holder = null;
    		if(view == null) {
    			view = LayoutInflater.from(mContext).inflate(R.layout.imagegallery_item, null);
    			holder = new Holder();
    			
    			holder.img = (SmartImageView) view.findViewById(R.id.dialog12_img);
    			holder.tv = (TextView) view.findViewById(R.id.dialog12_tv);
    			
    			view.setTag(holder);
    		}
    		else {
    			holder = (Holder) view.getTag();
    		}
    		holder.img.setImageUrl(mImageUrl[position]);
    		holder.tv.setText((position + 1) + "/" + mImageUrl.length);
    		
    		return view;
    	}

    	private class Holder {
    		SmartImageView img;
    		TextView tv;
    	}
    }
	
}
